<template>
  <div class="app-container" style="background: #f3f4f6;flex: 1;padding: 24px;box-sizing: border-box;overflow: hidden;">
    <div class="teacher_info_wrap">
      <div class="trainingPublish-title">培训基本信息</div>
      <div class="teacher_info_form">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="left">
          <el-row>
            <el-col :span="12">
              <el-form-item label="培训名称" prop="trainingTitle" style="margin-right: 20px;">
                <el-input v-model="form.trainingTitle" placeholder="请输入培训名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="培训类型" prop="trainingType">
                <el-select v-model="form.trainingType" placeholder="请选择培训类型">
                  <el-option
                    v-for="dict in dict.type.px_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="开始时间" prop="startTime" style="margin-right: 20px;">
                <el-date-picker clearable
                                v-model="form.startTime"
                                type="date"
                                style="width: 100%;"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择培训开始时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="结束时间" prop="endTime">
                <el-date-picker clearable
                                v-model="form.endTime"
                                type="date"
                                style="width: 100%;"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择培训结束时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>

            <el-col :span="12">
              <el-form-item label="学分发放" prop="trainingType" >
                <el-select v-model="form.trainingScoreType" placeholder="请选择学分发放">
                  <el-option
                    v-for="dict in dict.type.px_score_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="培训学分" prop="trainingScore" style="margin-right: 20px;">
                <el-input v-model="form.trainingScore" type="number" placeholder="请输入培训学分" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="培训地点" prop="trainingAddress">
                <el-input v-model="form.trainingAddress" placeholder="请输入培训地址" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="培训简介" prop="trainingNotes">
            <el-input v-model="form.trainingNotes" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="培训附件" prop="">
            <file-upload v-model="form.attachmentUrl" :limit="1" :fileSize ='50' :fileType="['word','excel','pdf','ppt','xlsx','pptx']" />
          </el-form-item>

        </el-form>
      </div>
      <div class="teacher_info_btn" style="display: flex;justify-content: flex-end">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">修改培训</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { addTeacher } from "@/api/classai/teacher";
import {addTraining, getTraining, updateTraining} from "@/api/classai/training";

export default {
  name: "AddTeacher",
  dicts: ['px_type','px_score_type'],
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        trainingTitle: [
          { required: true, message: "培训名称不能为空", trigger: "blur" }
        ],
        trainingType: [
          { required: true, message: "培训类型不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        trainingType: [
          { required: true, message: "学分发放不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created(){
    this.getList()
  },
  methods: {
    getList(){
      let id = this.$route.query.id
      getTraining(id).then(response => {
        this.form = response.data;
        this.form.trainingType = String( this.form.trainingType)
        this.form.trainingScoreType = String( this.form.trainingScoreType)
      });
    },
    // 取消按钮
    cancel() {
      this.reset();
      this.$router.back()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        trainingTitle: null,
        trainingType: null,
        startTime: null,
        endTime: null,
        trainingHours: null,
        trainingScore: null,
        trainingAddress: null,
        trainingNotes: null,
        attachmentUrl: null,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if(this.form.attachmentUrl && this.form.attachmentUrl.length>0) this.form.attachmentUrl = this.form.attachmentUrl[0].url
        if (valid) {
          updateTraining(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
          });
        }
      });
    },
  }
};
</script>
<style lang="scss" scoped>
.teacher_info_wrap {
  width: 100%;
  height: 100%;
  overflow: hidden;
  border-radius: 6px;
  background: #fff;
  box-sizing: border-box;
  border: 1px solid rgba(0, 0, 0, 0);
  box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.05);
  padding: 24px;
  .teacher_info_form {
    height: calc(100% - 60px);
    overflow-y: auto;
    ::v-deep {
      .el-form-item {
        display: flex;
        flex-direction: column;
      }
      .el-form-item__content {
        margin-left: 0!important;
        .el-select {
          width: 100%;
        }
      }
    }
  }
  .teacher_info_btn {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    height: 60px;
  }
}
.trainingPublish-title{
  font-size: 18px;
  font-weight: 500;
  line-height: 28px;
  color: #1F2937;
  margin-bottom: 24px;
}

</style>
